﻿@model ListControllerModel.Index
<div class="container-12">
    @AuctionHelper.GetSocketFlash()
    <ul class="auction clearfix">
        @foreach (var o in Model.Auctions)
        {
            <li id="auction-@(o.Id)" class="auction-item">
                <div class="auction-item-box">
                    <p class="icon">
                    </p>
                    <p class="name">@o.Name
                    </p>
                    <p class="picture">
                    </p>
                    <p class="marketprice">
                        <span>参考价：</span> <span class="price">@(o.MarketPrice.ToString("C"))</span>
                    </p>
                    <p>
                        <a href="javascript:void(0);" onclick="Sk.Auction.Fun.buy(@o.Id,'购买@(o.Name)');">购买
                        </a>
                    </p>
                    <p class="">
                        @if (DateTime.Now > o.StartDateTime)
                        {

                        }
                        else
                        {
                            <span></span>
                        }
                    </p>
                    <p class="currentprice">@(o.CurrentPrice.ToString("C"))
                    </p>
                    <p class="currentusernickname">@o.CurrentNickName
                    </p>
                </div>
            </li>
        }
    </ul>
</div>
@section js{
    @Url.Sk().StaticJs("zmqsocket")
}
<script type="text/javascript">
    var Notifier = function (options) {
        this.initialize(options);
    };
    Notifier.prototype = {
        constructor: Notifier,
        initialize: function (options) {
            this.options = { endPoint: '127.0.0.1', port: 5555 };
            this.options = $.extend(this.options, options || {});
            this.ondata = this.options.ondata || $.noop();
            this.ready = false;
            this.socket = getSocket(this);

            function getSocket(obj) {
                var socket = new ZmqSocket(0);
                socket.onopen = function () {
                    obj.ready = true;
                };
                socket.onmessage = function () {
                    if (socket.available()) {
                        var data = socket.recv();
                        var json = eval('(' + data + ')');
                        if ($.isFunction(obj.ondata)) {
                            obj.ondata(json);
                        }
                    }
                };
                socket.onclose = function () { alert('close'); };
                socket.onerror = function (msg) { alert(msg); };
                return socket;
            }
        }, connect: function () {
            this.socket.connect(this.options.endPoint, this.options.port);
        }
    };
</script>
<script type="text/javascript">
    var notifier = new Notifier();
    var timer = setInterval(check, 1000);
    var auctions=@(Model.JsonAuctions);
    function check() {
        var now = new Date();
        var begin = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 16, 55, 0);
        var end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 22, 0, 0);
        if (begin <= now && now <= end) {
            if (timer != null) {
                clearInterval(timer);
            }
            notifier.connect();
            timer = setInterval(update, 1000);
        }
    }
    function ondata(data){
         if (data.Status == true) {
                var bid = data.Data;
                var key = data.Data.AuctionId.toString();
                auctions[key].CurrentPrice = bid.CurrentPrice;
                auctions[key].Remain = auctions[key].Interval;
                auctions[key].UserId = bid.UserId;
                auctions[key].CurrentUser = bid.NickName;
            }
    }
    function update() {
        if (notifier.ready) {
            $.each(auctions, function (i, n) {
                console.log(n);
                var now = new Date();
                var counterId = "counter-" + i;
                var currentPriceId = "currentprice-" + i;
                var currentUserId = "currentuser-" + i;
                var currentPriceElement = document.getElementById(currentPriceId);
                var currentUserElement = document.getElementById(currentUserId);
                var remainElement = document.getElementById(counterId);
                //alert(n.BeginTime);
                if (now < n.BeginTime) {
                    $(remainElement).text('还没开始');
                }
                else if (n.CurrentPrice == 0) {
                    $(remainElement).text('等待出价');
                }
                else {
                    var remain = n.Remain - 1;
                    auctions[i.toString()].Remain = remain;
                    if (remain > 0) {
                        $(remainElement).text(remain);
                    }
                    else {
                        $(remainElement).text('xxx');
                    }
                }
                $(currentPriceElement).text(n.CurrentPrice);
                $(currentUserElement).text(n.CurrentUser); 
            });
        }
    }
 
</script>
